package com.aqumon.www.config;

import com.aqumon.www.job.GetAllBuilds;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {
    /**
     * FactoryBean可简化Bean的实例化过程:
     *      1. Spring通过FactoryBean封装了Bean的实例化过程.
     *      2. 它会将FactoryBean装配到Spring容器里.
     *      3. 然后将FactoryBean注入给其他的Bean.
     *      4. 该Bean得到的是FactoryBean所管理的对象实例
     */

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setAutoStartup(false);
        return factory;
    }

    // 配置JobDetail
    @Bean
    public JobDetailFactoryBean getAllBuildsDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(GetAllBuilds.class);
        factoryBean.setName("getAllBuilds");
        factoryBean.setGroup("getAllBuildsGroup");
        factoryBean.setDurability(true);// 设置任务长久保存
        factoryBean.setRequestsRecovery(true);// 设置任务可恢复
        return factoryBean;
    }

    // 配置Trigger(SimpleTriggerFactoryBean, CronTriggerFactoryBean)
    @Bean
    public SimpleTriggerFactoryBean getAllBuildsTrigger(JobDetail getAllBuildsDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(getAllBuildsDetail);// 设置任务的JobDetail
        factoryBean.setName("getAllBuildsTrigger");
        factoryBean.setGroup("getAllBuildsTriggerGroup");
        factoryBean.setRepeatInterval(60000);// 设置任务的执行频率(每三秒执行一次)
        factoryBean.setJobDataMap(new JobDataMap());// 设置存储Job的状态
        return factoryBean;
    }
}
